//
//  RMDateTableView.m
//  RMNote
//
//  Created by lxf on 16/4/15.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMDateTableView.h"
#import "RMDateCell.h"
#import "RMDateModel.h"
#import "RMDateHeaderView.h"

#define kRMDateCell @"RMDateCell"

@interface RMDateTableView ()<UITableViewDataSource,UITableViewDelegate>
{

    BOOL _isSelect;
    
   
}
@property (nonatomic, strong) NSDictionary *yearDic;
@property (nonatomic, strong) RMDateModel *model;
@property (nonatomic, strong) NSMutableDictionary *statusDic;
//@property (nonatomic, assign) NSInteger selectMonth;
@property (nonatomic, strong) NSDate *selectDate;;


@end

@implementation RMDateTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andDateModel:(RMDateModel *)model{
    if (self = [super initWithFrame:frame style:style]) {
        [self registerNib:[UINib nibWithNibName:@"RMDateCell" bundle:nil] forCellReuseIdentifier:kRMDateCell];
        [self registerNib:[UINib nibWithNibName:@"RMDateHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"RMDateHeaderView"];
        self.delegate = self;
        self.dataSource = self;
        self.model = model;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        for (NSString *year in self.model.years) {
            NSString *statuse = @"NO";
            [self.statusDic setValue:statuse forKey:year];
        }
        if (self.model.dateArr.count > 0) {
            self.selectDate = self.model.dateArr[0];
        }
        
        self.showsVerticalScrollIndicator = NO;
        [[NSNotificationCenter defaultCenter]addObserverForName:@"sectionChange" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
            NSDate *date = note.object;
            _selectDate = date;
            
            NSInteger selectYear = [self.model.years indexOfObject:_selectDate.year];
            NSArray *moth = [self.model monthsForYear:_selectDate.year];
            NSInteger selectMonth = [moth indexOfObject:_selectDate.month];
            NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:selectMonth inSection:selectYear];
            [self selectRowAtIndexPath:indexPath1 animated:YES scrollPosition:(UITableViewScrollPositionNone)];
            
        }];
        
    }
    return self;
}

#pragma mark - 懒加载

- (NSDictionary *)yearDic{
    if (!_yearDic) {
        _yearDic = [NSMutableDictionary dictionary];
    }
    return _yearDic;
}

- (NSMutableDictionary *)statusDic{
    if (!_statusDic) {
        _statusDic = [NSMutableDictionary dictionary];
    }
    return _statusDic;
}


#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.model.years.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSMutableArray *monthArr = [self.model monthsForYear:self.model.years[section]].mutableCopy;
    
    return monthArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *nowYear = self.model.years[indexPath.section];
    NSMutableArray *monthArr = [self.model monthsForYear:nowYear].mutableCopy;
    NSString *month = monthArr[indexPath.row];
    RMDateCell *cell= [tableView dequeueReusableCellWithIdentifier:kRMDateCell forIndexPath:indexPath];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.circleImage.layer.cornerRadius=3;
    cell.monthLabel.text = [NSString stringWithFormat:@"%@",month];
    return cell;
}


//设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 55;
}

//自定义区头 把区头model 创建的view写这里
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    
    RMDateHeaderView *view = [[[NSBundle mainBundle]loadNibNamed:@"RMDateHeaderView" owner:nil options:nil] lastObject];
    [view.yearBtn addTarget:self action:@selector(expand:) forControlEvents:(UIControlEventTouchUpInside)];
    view.yearBtn.tag = section;
    [view.yearBtn setTitle:self.model.years[section] forState:(UIControlStateNormal)];

    
    return view;
    
}

- (void)expand:(UIButton*)btn
{
    
    NSString *status = [self.statusDic objectForKey:self.model.years[btn.tag]];
    if ([status isEqualToString:@"NO"]) {
        status = @"YES";
        [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }else{
        status = @"NO";
        [btn setTitleColor:kRMMainLineColor forState:(UIControlStateNormal)];
    }
    [self.statusDic setValue:status forKey:self.model.years[btn.tag]];

    NSArray *moth = [self.model monthsForYear:self.model.years[btn.tag]];
   
    
    if (!moth) {
        return;
    }


    NSMutableArray *indexArr = [NSMutableArray array];
    for (int i = 0; i < moth.count; i ++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:btn.tag];
        [indexArr addObject:indexPath];
    }
    
    [self reloadRowsAtIndexPaths:indexArr withRowAnimation:(UITableViewRowAnimationNone)];

    
    NSInteger selectYear = [self.model.years indexOfObject:_selectDate.year];
    NSArray *moth1 = [self.model monthsForYear:_selectDate.year];
    NSInteger selectMonth = [moth1 indexOfObject:_selectDate.month];
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:selectMonth inSection:selectYear];
    [self selectRowAtIndexPath:indexPath1 animated:YES scrollPosition:(UITableViewScrollPositionNone)];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *status = [self.statusDic objectForKey:self.model.years[indexPath.section]];

    if ([status isEqualToString:@"YES"]) {
        
        return 58;
    }else{
       // !!!WARNING: 当返回高度是0时,由于 自定义 cell 中默认 label imageView 是有高度的,所以会有警告,约束和返回高度冲突
        return 0;
    }

    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *year = self.model.years[indexPath.section];
    NSMutableArray *monthArr = [self.model monthsForYear:year].mutableCopy;
    NSString *month = monthArr[indexPath.row]; 
    NSLog(@"%@年,%@月",year,month);

    
    NSString *month_year = [NSString stringWithFormat:@"%@-%@",month,year];
    
    if (self.model.month_yearArr.count >0) {
        NSInteger index = [self.model.month_yearArr indexOfObject:month_year];
        NSString *indexStr = [NSString stringWithFormat:@"%ld",(long)index];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"change" object:indexStr];

    }
//    RMDateCell *cell = [self cellForRowAtIndexPath:indexPath];
//    [cell changeColor:YES];
//    self.postYearAndMonth(year,month);

    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

//    cell.backgroundColor = [UIColor redColor];
    cell.alpha = 0;
    [UIView animateWithDuration:1 animations:^{
        cell.alpha = 1;
    }];
    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"sectionChange" object:nil];
}


@end
